<?php
/**
 * 
 * @author Sergio
 *
 */
class TMembre extends SQLite3
{

	public function __construct()	{
		if(file_exists(PATH_BDD.'.db')) {
			$this->open(PATH_BDD.'.db');
		}
	}
	/**
	 * Verifier l'existance de l'utilisateur 
	 * @param unknown $login
	 * @param unknown $password
	 * @return SQLite3Result
	 */
	public static function isUserExist($login, $password) {
		$db = new TMembre();
		if (!empty($login) && !empty($password)) {
			$result = $db->query('SELECT count(*) as isUserExist FROM membre WHERE login="' . $login . '" AND password="' . $password . '"');
			$result = $result->fetchArray(SQLITE3_ASSOC);
			$index = $result['isUserExist'];
			error_log("[TMembre.php] Index ==> $index \n", 3, "/var/www/application/tacks-php.log");
		}
		if ($index == 1) {
			error_log("[TMembre.php] TRUE\n", 3, "/var/www/application/tacks-php.log");
			return true;

		}else {
			error_log("[TMembre.php] FASLE\n", 3, "/var/www/application/tacks-php.log");
			return false;
		}
	}

	public static function getUserDroit($login) {

		$db = new TMembre();
		if (!empty($login)) {
			$result = $db->query('SELECT droit FROM membre WHERE login="' . $login . '"');
			$result = $result->fetchArray(SQLITE3_ASSOC);
			$result = $result['droit'];
		}
		return $result;
	}

	public static function getUserFamillyId($login) {

		$db = new TMembre();
		if (!empty($login)) {
			$result = $db->query('SELECT id_famille FROM membre WHERE login="' . $login . '"');
			$result = $result->fetchArray(SQLITE3_ASSOC);
			$result = $result['id_famille'];
		}
		return $result;
	}

	public static function getIdMembre($login) {
		$db = new TMembre();
                if (!empty($login)) {
                        $result = $db->query('SELECT id_membre FROM membre WHERE login="' . $login . '"');
                        $result = $result->fetchArray(SQLITE3_ASSOC);
                        $result = $result['id_membre'];
                }
                return $result;
	}

	public static function addUser(&$user_array) {

		$db = new TMembre();

		$values = "{$user_array['nom']} {$user_array['prenom']} {$user_array['email']} {$user_array['tel']} {$user_array['droit']} {$user_array['login']} {$user_array['password']}";
		error_log("[TMembre.php] DEGUB: User array values: $values \n", 3, "/var/www/application/tacks-php.log");
		// on recherche si ce login est déjà utilisé par un autre membre
		$isUserExist = $db->isUserExist($user_array['login'], $user_array['password']);

		if ($isUserExist == false){
			$sql = 'INSERT INTO`membre` (`nom`, `prenom`, `email`, `droit`, `tel`, `id_famille`, `password`, `login`) 
				VALUES ("'.$user_array['nom'].'", "'.$user_array['prenom'].'", "'.$user_array['email'].'", "'.$user_array['droit'].'", "'.$user_array['tel'].'", "1", "'.$user_array['password'].'", "'.$user_array['login'].'")';
			error_log("[TMembre.php] DEGUB: SQL Request addUser : $sql \n", 3, "/var/www/application/tacks-php.log");
			$result = $db->query($sql);

			$isUserExist = $db->isUserExist($user_array['login'], $user_array['password']);
			if ($isUserExist) {
				error_log("[TMembre.php] New User created {$user_array['login']} \n", 3, "/var/www/application/tacks-php.log");
				return true;
			} else {
				return false;
			}
		}else {
			error_log("[TMembre.php] Error: user already exist ! \n", 3, "/var/www/application/tacks-php.log");
			return "Error: user already exist !";
		}
	}

	public static function deleteUser($login) {
		$db = new TMembre();
                if (!empty($login)) {
                        $result = $db->query('DELETE FROM membre WHERE login = "' . $login . '"');
                }
                if ($result) {
                        return true;
                }else {
                        return false;
                }

	}

	public static function updateUserDroit($login, $droit) {
                $db = new TMembre();
                if (!empty($login) && !empty($droit)) {
                        $result = $db->query('UPDATE membre SET droit = "' . $droit . '" WHERE login = "' . $login . '"');
                }
                if ($result) {
                		return true;
                }else {
                        return false;
                }

    }

	public static function UpdateUserPassword($login, $password) {
        	$db = new TMembre();
                if (!empty($login) && !empty($password)) {

                        $sql = 'UPDATE membre SET password = "' . $password . '" WHERE login = "' . $login . '"';
                	$result = $db->query($sql);
			//error_log("[TMembre.php] DEGUB: updateUserPassword --> resultBoolean : $result, SQL: $sql \n", 3, "/var/www/application/tacks-php.log");
		}

		if (!($result instanceof Sqlite3Result)) {
			error_log("[TMembre.php] DEGUB: updateUserPassword --> Query successful. \n", 3, "/var/www/application/tacks-php.log");
			return true;
		} else {
    			$result->fetchArray(); // This will throw an error.
			return false;
		}
	}

	public static function updateUserInfos($login, $email, $tel) {
                $db = new TMembre();
                if (!empty($login) && !empty($email) && !empty($tel)) {
                        $sql = 'UPDATE membre SET email = "' . $email . '", tel= "' . $tel . '"  WHERE login = "' . $login . '"';
			$result = $db->query($sql);
                	error_log("[TMembre.php] DEGUB: updateUserINfos --> resultBoolean : $result, SQL: $sql \n", 3, "/var/www/application/tacks-php.log");
               	}

  	}


	public static function getFamillyListUser($id_famille) {
		$db = new TMembre();
		$results =  $db->query('SELECT login, nom, prenom, droit FROM membre WHERE id_famille="' . $id_famille . '"');

		
		$result = array();
		$i = 0;
		while ($row = $results->fetchArray()) {
			$result[$i]['login'] = $row['login'];
			$result[$i]['nom'] = $row['nom'];
			$result[$i]['prenom'] = $row['prenom'];
			$result[$i]['droit'] = $row['droit'];
			$res = $result[$i]['nom'];
			$i +=1;
		}

		$results = json_encode($result);
		error_log("[TMembre.php] DEBUG: gettFamillyListUser: $results \n", 3, "/var/www/application/tacks-php.log");
		return $results;
	
	}
	
	public static function getUserInfos($login) {
		$db = new TMembre();
		$results =  $db->query('SELECT nom, prenom, email, tel FROM membre WHERE login="' . $login . '"');
	
		$result = array();
		$i = 0;
		while ($row = $results->fetchArray()) {
			$result[$i]['nom'] = $row['nom'];
			$result[$i]['prenom'] = $row['prenom'];
			$result[$i]['email'] = $row['email'];
			$result[$i]['tel'] = $row['tel'];
			$i +=1;
		}
		$results = json_encode($result);
		error_log("[TMembre.php] DEBUG: getUserInfos: $results \n", 3, "/var/www/application/tacks-php.log");
		return $results;
	}
	
	public static function getAllUser() {
		//TODO - get all user - root only
	}
	
}
?>
